﻿// Copyright (c) 2012 - 2021 Jared Zheng <jaredz at outlook dot com>
// Buddha Framework is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//          http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
//
// Create : 2012-12-01 version 0.1 Jared Zheng <jaredz at outlook dot com>
// Update : 2021-03-19 version 4.0.0 Jared Zheng <jaredz at outlook dot com>

#ifndef __SERVER_LOADER_H__
#define __SERVER_LOADER_H__

#pragma once

#include "singleton.h"
#include "MainHandler.h"

///////////////////////////////////////////////////////////////////////////////////////////////////////////
/// 服务加载
class CServerLoader : public MObject
{
public:
    DECLARE_CLASS_LOADER( CServerLoader, NET )
    DECLARE_CLASS_LOADER( CServerLoader, Frame )
#if (__PLATFORM_TARGET__ == PLATFORM_TARGET_WINDOWS)
public:
    static Int ThreadExceptHandler(TId tThreadId, Int nThreadType, Int nThreadRoutine, void* pRoutine, struct _EXCEPTION_POINTERS* pExceptInfo);
#endif
public:
    CServerLoader(void);
    ~CServerLoader(void);

    bool Init(PCXStr pszCmd = nullptr);
    bool Init(Int nArgc, PXStr* ppszArgv);
    void Exit(void);

    void Start(void);
    void Stop(void);
private:
    bool InitSystem(void);
    void ExitSystem(void);

    void DumpPlatform(void);
    PCXStr GetOSType(UInt uOSType);
public:
    CFileLogPipe        m_FileLog;
    CSyncEvent          m_SyncEvent;

    CServerConfig       m_Config;

    CNetworkPtr         m_NetworkPtr;

    CMainHandler        m_MainHandler;
};

///////////////////////////////////////////////////////////////////////////////////////////////////////////
/// 服务加载器子系统加载器单件
class   CServerLoaderSingleton : public CTSingleton<CServerLoader> {};
typedef CServerLoaderSingleton   GServerLoader;
#define GServerLoaderInst        CServerLoaderSingleton::GetInstance()

#endif // __SERVER_LOADER_H__
